package springboot1706.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import springboot1706.bean.Cart;
import springboot1706.bean.Goods;
import springboot1706.bean.Gw;
import springboot1706.service.impl.GwServiceImpl;

@Controller
@RequestMapping("/gw")
public class GwController {
	Logger log = LoggerFactory.getLogger(TestController.class);
	
	@Autowired
	   private GwServiceImpl gwServiceImpl;
	
	/**
	 * 得到用户列表
	 * @return
	 */
 
	@RequestMapping("/list")
	public String list(HttpSession session){
		Gw gw = new Gw();
		//log.debug("得到用户名"+userName);
		String userName = (String) session.getAttribute("username");
		log.debug(userName+"===========================================");
		gw.setUserName(userName);
		List<Gw> list2 = gwServiceImpl.getAll(gw);
		
		session.setAttribute("list2", list2);
		return "gw";
	}
	

	
	
	/**
	 * 加购物车
	 * @return
	 * @throws UnsupportedEncodingExaception 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/add")
	public String add(HttpSession session,
    @RequestParam(value="sjname",required=false)String sjname,
	@RequestParam(value="sjaddress",required=false) String sjaddress, 
	@RequestParam(value="sjphone",required=false) String sjphone
	)throws UnsupportedEncodingException{
//		String sjname = request.getParameter("sjname");
//		String sjaddress = request.getParameter("sjaddress");
//		String sjphone = request.getParameter("sjphone");
		@SuppressWarnings("unused")
		List<Cart> cartlist = (List<Cart>) session.getAttribute("list2");
		String userName = (String) session.getAttribute("username");
		for ( Cart cart:cartlist ) {
			Gw gw = new Gw();
			
			gw.setGid(cart.getGid());
			gw.setImg(cart.getImg());
			gw.setName(cart.getName());
			gw.setType(cart.getType());
			gw.setNum(cart.getNum());
			cart.setUserName(userName);
			gw.setUserName(cart.getUserName());
			gw.setPrice(cart.getPrice());
			
			gw.setZt(cart.getZt());
			gw.setSjname(sjname);
			gw.setSjaddress(sjaddress);
			gw.setSjphone(sjphone);
	
			gw.setZt(cart.getZt());
			List<Gw> list = gwServiceImpl.getGwByGid(gw);
			
			
		Goods goods=new Goods();
			goods.setName(cart.getName());
			List<Goods> list1=gwServiceImpl.getGoods(goods);	
			goods=list1.get(0);
			int stock=goods.getStock()-cart.getNum(); 
			if (list == null || list.size() < 1) {
				int result1 = gwServiceImpl.insert(gw);
				
				
				if (result1 >= 1) {
					 
					int result2 = gwServiceImpl.deleteAll(cart);
						
					
					goods.setStock(stock);
					int result3=gwServiceImpl.updateKc(goods);
				
					session.setAttribute("success", "");
					
				}
			} else {
				for (Gw gw2 : list) {
					int num1 = gw2.getNum() + gw.getNum();
					gw.setNum(num1);
					int result = gwServiceImpl.update(gw);
					
					if (result >= 1) {
						
						int result2 = gwServiceImpl.deleteAll(cart); 							
			//	goods.setStock(stock);
			//			int result3=gwServiceImpl.updateKc(goods);
						
						session.setAttribute("success", "");
					
					}
					
				}
			}			
		}
		return "redirect:/gw/list";		
	}

	/**
	 * 删除订单
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/del")
	public String del(HttpServletRequest request) throws UnsupportedEncodingException{
		
		String userName = (String) request.getSession().getAttribute("username");
		String gid = request.getParameter("gid");
		String type = request.getParameter("type");
		Gw gw = new Gw();
		gw.setGid(gid);
		gw.setType(type);
		gw.setUserName(userName);
		int result = gwServiceImpl.delete(gw);
		if (result != 0) {
			request.setAttribute("success", "success");
			return "redirect:/gw/list";
		} else {
			request.setAttribute("error", "error");
			return "redirect:/gw/list";
		}
	}
	
	
	
	@RequestMapping("/ys")
	public String ys(HttpServletRequest request) throws UnsupportedEncodingException{
		String userName = (String) request.getSession().getAttribute("username");
		String gid = request.getParameter("gid");
		String type = request.getParameter("type");
		Gw gw = new Gw();
		gw.setGid(gid);
		gw.setType(type);
		gw.setUserName(userName);
	String zt="2";
	gw.setZt(zt);
	int result=gwServiceImpl.updateZt(gw);
			
			if (result >= 1) {
				request.setAttribute("success", "成功");
			}
		return "redirect:/gw/list";
	
	}

	
	
	
}
